package com.woniuxy.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.his.entity.Case;
import com.woniuxy.his.entity.Prescription;
import com.woniuxy.his.exception.GlobalExceptionHandle;
import com.woniuxy.his.exception.ParamExceptionHandle;
import com.woniuxy.his.mapper.CaseMapper;
import com.woniuxy.his.mapper.PresDetailMapper;
import com.woniuxy.his.mapper.PrescriptionMapper;
import com.woniuxy.his.service.ICaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.his.vo.InspectVo;
import com.woniuxy.his.vo.OutMedicineDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 病历表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@Service
@Slf4j
public class CaseServiceImpl extends ServiceImpl<CaseMapper, Case> implements ICaseService {

    //病例
    @Resource
    private CaseMapper caseMapper;

    //处方
    @Resource
    private PrescriptionMapper prescriptionMapper;

    //处方详情
    @Resource
    private PresDetailMapper presDetailMapper;

    @Override
    public List<OutMedicineDetailVo> getAllMedicineByPrescriptionId(Integer prescriptionId, Date startTime, Date stopTime) {
       //判断当天是否有挂号信息及是否有添加病历
        QueryWrapper<Case> caseIsAddWrapperrapper = new QueryWrapper<>();
        caseIsAddWrapperrapper.between("case_add_time",startTime,stopTime);
        List<Case> cases = caseMapper.selectList(caseIsAddWrapperrapper);
        if (cases.size()==0) {
            throw new ParamExceptionHandle();
        }
        //获取对应的病历id
//        QueryWrapper<Case> caseQueryWrapperrapper = new QueryWrapper<>();
//        caseQueryWrapperrapper.eq("patient_id",patientId).between("case_add_time",startTime,stopTime);
//        Case aCase = caseMapper.selectOne(caseQueryWrapperrapper);
//        Integer caseId = aCase.getCaseId();
        //根据病历id获取处方id
//        QueryWrapper<Prescription> prescriptionQueryWrapper = new QueryWrapper<>();
//        prescriptionQueryWrapper.eq("case_id",caseId);
//        Prescription prescription = prescriptionMapper.selectOne(prescriptionQueryWrapper);
//        Integer prescriptionId = prescription.getPrescriptionId();
        //根据处方id获取所有的药品详情
        List<OutMedicineDetailVo> medicenesDetails = presDetailMapper.getMedicenesDetail(prescriptionId);

        return medicenesDetails;
    }

    /**
     * 根据患者名称获得患者检查项目
     * @param patientName
     * @return
     */
    @Override
    public List<InspectVo> findInspectByPatientName(String patientName) {

        List<InspectVo> inspectVos = caseMapper.findInspectByPatientName(patientName);

        return inspectVos;
    }
}
